﻿using HalconDotNet;
using NeuroCheck.PlugInLib.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CLVisionHalconNC620.Transform
{
    public class RegisterTransform
    {

        /// <summary>
        /// 数组写入寄存器格   [[字符串;字符串;字符串；...  ]]
        /// </summary>
        /// <param name="registerId"></param>
        /// <param name="htuple"></param>
        public static void WriteTupleToRegister(int registerId, HTuple htuple)
        {
            List<string> tempList = new List<string>();
            for (int i = 0; i < htuple.Length; i++)
            {
                HTupleType valueType = htuple[i].Type;
                switch (valueType.ToString().ToLower())
                {
                    case "long":
                        tempList.Add(htuple[i].L.ToString());
                        break;
                    case "integer":
                        tempList.Add(htuple[i].I.ToString());
                        break;
                    case "string":
                        string str = "\"" + htuple[i].S + "\"";
                        tempList.Add(str);
                        break;
                    case "double":
                        tempList.Add(htuple[i].D.ToString());
                        break;
                }
            }
            //连接字符串
            string connectStr = string.Join(";", tempList);
            //添加起止符
            string resStr = "[[" + connectStr + "]]";
            //写入寄存器
            NcPIDataRegisterManager.WriteToRegister(registerId, resStr);
        }

        public static void ReadTupleFromRegister(int registerId, out HTuple htuple, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = "";
            try
            {
                HTuple tempTuple = new HTuple();
                NcPIDataRegisterManager.ReadFromRegister(registerId, out object obj);
                string tempStr = obj as string;
                tempStr = tempStr.Replace("[[", "").Replace("]]", "");
                string[] tempArray = tempStr.Split(';');
                for (int i = 0; i < tempArray.Length; i++)
                {
                    tempTuple[i] = tempArray[i].Replace("\"", "");
                    if (tempArray[i].Contains("\""))  //string
                    {
                        tempTuple[i] = tempArray[i].Replace("\"", ""); ;
                    }
                    else
                    {
                        if (int.TryParse(tempArray[i], out int resultInt))
                        {
                            tempTuple[i] = resultInt;
                        }
                        else if (double.TryParse(tempArray[i], out double resultDouble))
                        {
                            tempTuple[i] = resultDouble;
                        }
                    }
                }
                htuple = tempTuple;
            }
            catch (Exception ex)
            {
                htuple = null;
                ErrorCode = -1;
                ErrorText = ex.Message;
            }

        }

    }
}
